<?php

namespace backend\controllers;

use common\models\ding\Check;
use common\models\ding\Name;
use common\models\ding\Talk;
use common\models\personnel\ByProject;
use common\models\personnel\Group;
use common\models\personnel\Leave;
use common\models\personnel\Personnel;
use Yii;
use backend\forms\ClearCache;
use common\helpers\ResultHelper;

/**
 * 主控制器
 *
 * Class MainController
 * @package backend\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class MainController extends BaseController
{
    /**
     * 系统首页
     *
     * @return string
     */
    public $modelClass = Check::class;
    public $nameModel = Personnel::class;
    public $leaveModel = Leave::class;
    public $talkModel = Talk::class;
    public $groupClass = ByProject::class;
    public function actionIndex()
    {
        return $this->renderPartial($this->action->id, [
        ]);
    }

    /**
     * 子框架默认主页
     *
     * @return string
     */
    public function actionSystem()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        $firstday = strtotime(date('Y-m-01', time()));//本月第一天
        $fivetday = strtotime(date('Y-m-d', time()-432000));//本月当天前五天
        $lastday = strtotime(date('Y-m-d',time()));//本月当天
        $late = $this->modelClass::find()
            ->where(['timeresult'=>'Late'])
            ->andWhere(['>=','userCheckTime',$firstday])
            ->andFilterWhere(['<=','userCheckTime',$lastday])
            ->count('id');
        $people = $this->nameModel::find()
            ->where(['status'=>1])
            ->count('personnel_id');
        $leave = $this->leaveModel::find()
            ->where(['status'=>1])
            ->andwhere(['report_time'=>null])
           // ->where(['type'=>1||2])
            ->count('id');
        $group = $this->groupClass::find()
            ->count('id');
        $end = $this->groupClass::find()
            ->where(['status'=>2])
            ->count('id');
        $weekly = $this->talkModel::find()
            ->andWhere(['>=','created_at',$fivetday])
            ->andFilterWhere(['<=','created_at',$lastday])
            ->count('id');
        return $this->render($this->action->id, [
            'late' => $late,
            'people' => $people,
            'group' => $group,
            'end' => $end,
            'leave' => $leave,
            'weekly' => $weekly,
            'memberCount' => Yii::$app->services->member->getCount($merchant_id),
            'memberAccount' => Yii::$app->services->memberAccount->getSum($merchant_id),
        ]);
    }

    /**
     * 用户指定时间内数量
     *
     * @param $type
     * @return array
     */
    public function actionMemberBetweenCount($type)
    {
        $data = Yii::$app->services->member->getBetweenCountStat($type);

        return ResultHelper::json(200, '获取成功', $data);
    }

    /**
     * 充值统计
     *
     * @param $type
     * @return array
     */
    public function actionMemberRechargeStat($type)
    {
        $data = Yii::$app->services->memberCreditsLog->getRechargeStat($type);

        return ResultHelper::json(200, '获取成功', $data);
    }

    /**
     * 用户指定时间内消费日志
     *
     * @param $type
     * @return array
     */
    public function actionMemberCreditsLogBetweenCount($type)
    {
        $data = Yii::$app->services->memberCreditsLog->getBetweenCountStat($type);

        return ResultHelper::json(200, '获取成功', $data);
    }

    /**
     * 清理缓存
     *
     * @return string
     */
    public function actionClearCache()
    {
        $model = new ClearCache();
        if ($model->load(Yii::$app->request->post())) {
            return $model->save()
                ? $this->message('清理成功', $this->refresh())
                : $this->message($this->getError($model), $this->refresh(), 'error');
        }

        return $this->render($this->action->id, [
            'model' => $model
        ]);
    }
}